package no.ntnu.item.ttm4115.termproject.qasessionagent;

import no.ntnu.item.ttm4115.termproject.UpStartToQASession;
import no.ntnu.item.ttm4115.termproject.GetTreasureInfo;
import no.tellu.common.javaframe.ActorAddress;
import no.ntnu.item.ttm4115.termproject.TreasureInfoToQaSession;
import no.tellu.common.javaframe.messages.RoleRequestMsg;
import no.tellu.common.actorframe.messages.RoleConfirmMsg;
import no.tellu.common.actorframe.messages.RoleReleaseMsg;
import no.ntnu.item.ttm4115.termproject.InfoToQuestion;
import no.ntnu.item.ttm4115.termproject.CorrectQuestionAnswer;
import no.ntnu.item.ttm4115.termproject.RegisterScoreForTreasure;
import no.tellu.common.actorframe.messages.RoleRemoveMsg;
import no.tellu.common.actorframe.messages.RoleDeniedMsg;


public class QaSessionAgentActions extends Object {

	public static void askForTreasureInfo(UpStartToQASession signal, QaSessionAgentSM asm){
		asm.myGroupGameAddress = signal.getSenderRole();
		asm.myGameManagerAddress =signal.gameManagerAgent;
		System.out.println(signal.gameManagerAgent);
		asm.sendMessage(new GetTreasureInfo(asm.getContext().getParentAddress()), signal.gameManagerAgent);
	}

	public static void startUpQuestionRole(TreasureInfoToQaSession signal, QaSessionAgentSM asm){
		asm.treasureForQA = signal.treasure;
		String question = "Question"+ (int)((Math.random()*1000)+1);    //asm.questionNumber;
		System.out.println("id*** "+question+"  "+asm.treasureForQA.getQuestions().get(asm.questionNumber).getType());
		asm.sendMessage(new RoleRequestMsg(question,asm.treasureForQA.getQuestions().get(asm.currentQuestion).getType()), asm.getMyActorAddress());
		asm.questionNumber++;
		System.out.println(asm.questionNumber);
		System.out.println(asm.currentQuestion);
		System.out.println(asm.treasureForQA.getClue());
	}

	public static void giveInfoToQuestionAgent(RoleConfirmMsg signal, QaSessionAgentSM asm){    
		System.out.println("her sendes info til question agent");
		asm.sendMessage(new InfoToQuestion(asm.myGroupGameAddress,asm.treasureForQA.getQuestions().elementAt(asm.currentQuestion)), signal.getSenderRole());
	}


	public static boolean moreQuestions(CorrectQuestionAnswer signal, QaSessionAgentSM asm){
		if(asm.currentQuestion<asm.treasureForQA.getQuestions().size()){
			return true;


		}
		else{
			return false;
		}
	}

	public static void startUpNewQuestionAgent(QaSessionAgentSM asm){

		System.out.println("more questions...");
		System.out.println(asm.questionNumber);

		asm.treasureForQA.getQuestions().elementAt(asm.currentQuestion).getType();

		String qquestion = "Question"+ (int)((Math.random()*1000)+1);  //asm.questionNumber;    

		System.out.println("***** new id "+qquestion+" type: "+asm.treasureForQA.getQuestions().elementAt(asm.currentQuestion).getType()+" to** "+asm.getMyActorAddress());

		asm.sendMessage(new RoleRequestMsg(qquestion,asm.treasureForQA.getQuestions().elementAt(asm.currentQuestion).getType()), asm.getMyActorAddress());
		asm.questionNumber++;
	}

	public static void registerScore(CorrectQuestionAnswer signal,QaSessionAgentSM asm){
		int pointsForThisSession = 0 ;
		for(int i=0; i<asm.pointsForQuestions.size(); i++){
			pointsForThisSession =pointsForThisSession +(Integer) asm.pointsForQuestions.get(i);
		}
		asm.sendMessage(new RegisterScoreForTreasure(pointsForThisSession,asm.pointsForQuestions,signal.groupAddress), asm.myGameManagerAddress);
	}

	public static void updateQuestionInfo(CorrectQuestionAnswer signal, QaSessionAgentSM asm){
		asm.pointsForQuestions.add(asm.currentQuestion, signal.pointsAchivedOnForQuestion);
		asm.currentQuestion++;

		//asm.sendMessage(new RoleRemoveMsg(), signal.getSenderRole());
	}

	public static void gotRoleDenied(RoleDeniedMsg signal, QaSessionAgentSM asm){
		System.out.println("Recived roledenied");
	}



}
